package com.zxg.zbc.dal.dao.concurrent;

import com.taobao.common.dao.persistence.DBRoute;
import com.taobao.common.dao.persistence.SqlMapBaseDAO;
import com.zxg.zbc.dal.dao.exception.DAOException;
import com.zxg.zbc.dal.dataobject.concurrent.LastProcessed;
import com.zxg.zbc.dal.dataobject.concurrent.Lock;
import com.zxg.zbc.dal.dataobject.concurrent.Lock.ConcurrentType;

public class IbatisConcurrentDao extends SqlMapBaseDAO implements ConcurrentDao {

  @Override
  public boolean lock(String host, ConcurrentType type, Integer timeout) throws DAOException {
    try {
      Lock lock = new Lock();
      if (host == null || type == null)
        throw new DAOException("error params  is null");
      if (timeout != null)
        lock.setTimeout(timeout);
      lock.setHost(host);
      lock.setType(type);

      return this.executeUpdate("Lock.doLock", lock, DBRoute.getAssistantRoute()) > 0;
    } catch (Exception e) {
      throw new DAOException("Lock.doLock", e);
    }
  }

  @Override
  public boolean unlock(String host, ConcurrentType type) throws DAOException {
    try {
      Lock lock = new Lock();
      if (host == null || type == null)
        throw new DAOException("error params  is null");
      lock.setHost(host);
      lock.setType(type);
      return this.executeUpdate("Lock.unLock", lock, DBRoute.getAssistantRoute()) > 0;
    } catch (Exception e) {
      throw new DAOException("Lock.unLock", e);
    }
  }

  @Override
  public Lock getLockByType(ConcurrentType type) throws DAOException {
    if (type == null)
      throw new DAOException("error params  is null");
    try {
      Lock lock = (Lock) this.executeQueryForObject("Lock.getLockByType", type.getValue(), DBRoute.getAssistantRoute());
      return lock;
    } catch (Exception e) {
      throw new DAOException("Lock.insertLockDO", e);
    }

  }

  @Override
  public Lock insertLock(Lock lock) throws DAOException {
    if (lock == null)
      throw new DAOException("error params  is null");
    try {
      Long id = (Long) this.executeInsert("Lock.insertLockDO", lock, DBRoute.getAssistantRoute());
      lock.setId(id);
    } catch (Exception e) {
      throw new DAOException("Lock.insertLockDO", e);
    }
    return lock;
  }

  @Override
  public LastProcessed getLastProcessedByType(ConcurrentType type) throws DAOException {
    if (type == null)
      throw new DAOException("error params  is null");
    try {
      LastProcessed item = (LastProcessed) this.executeQueryForObject("Lastprocessed.getLastprocessedByType", type.getValue(), DBRoute.getAssistantRoute());
      return item;
    } catch (Exception e) {
      throw new DAOException("Lastprocessed.getLastprocessedByType", e);
    }
  }

  @Override
  public Integer updateLastProcessed(LastProcessed lastProcessed) throws DAOException {
    try {

      if (lastProcessed == null)
        throw new DAOException("error params  is null");

      return this.executeUpdate("Lastprocessed.update", lastProcessed, DBRoute.getAssistantRoute());
    } catch (Exception e) {
      throw new DAOException("Lastprocessed.update", e);
    }
  }

  @Override
  public LastProcessed insertLastProcessed(LastProcessed lastProcessed) throws DAOException {
    if (lastProcessed == null)
      throw new DAOException("error params  is null");
    try {
      Long id = (Long) this.executeInsert("Lastprocessed.insertLastprocessedDO", lastProcessed, DBRoute.getAssistantRoute());
      lastProcessed.setId(id);
    } catch (Exception e) {
      throw new DAOException("Lastprocessed.insertLastprocessedDO", e);
    }
    return lastProcessed;
  }

}
